/**
 * @author zhounan 
 * @date 2024年5月23日
 * @version V1.0
 */
package org.zn.etl.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zn.etl.job.context.JobExecInstanceContext;
import org.zn.etl.job.dao.CheckPointMapper;
import org.zn.etl.job.dao.JobExecInstanceMapper;
import org.zn.etl.job.entity.CheckPoint;
import org.zn.etl.job.entity.po.JobExecInstancePo;
import org.zn.etl.misc.BeanCopierUtils;
import org.zn.etl.misc.UuidUtils;
import org.zn.etl.service.EtlRunTimePersistService;

/**
 * @author zhounan 
 * @date 2024年5月23日
 * @version V1.0
 */
@Service
public class EtlRunTimePersistServiceImpl  implements EtlRunTimePersistService{
	
	@Autowired
	private CheckPointMapper   checkpointMapper;
	@Autowired
	private JobExecInstanceMapper   jobExecInsMapper;

	@Override
	public void saveCheckPoint(CheckPoint cp) {
		// TODO Auto-generated method stub
		
		if(cp.getId()==null) {

			//cp.setId(UuidUtils.generateIdString());
			cp.setCreateTime(new Date());
			checkpointMapper.insert(cp);			
			
		}else{
			cp.setUpdateTime(new Date());
			checkpointMapper.updateByPrimaryKey(cp);			
		}
		
	}

	@Override
	public void saveExecInstance(JobExecInstancePo ins) {
		// TODO Auto-generated method stub
		
		if(ins.getId()==null) {
			//ins.setId(UuidUtils.generateIdString());
			ins.setCreateTime(new Date());
			jobExecInsMapper.insert(ins);
		}
		else {
			ins.setUpdateTime(new Date());
			jobExecInsMapper.updateByPrimaryKey(ins);
		}
		
	}

	@Override
	public List<CheckPoint> loadCheckPoint(Long execInsId) {
		// TODO Auto-generated method stub
		
		List<CheckPoint> list=checkpointMapper.selectByInsId(execInsId);
		
		return list;
	}

	@Override
	public JobExecInstanceContext queryExecInstance(Long execInsId) {
		// TODO Auto-generated method stub
		
		JobExecInstancePo ji=jobExecInsMapper.selectByPrimaryKey(execInsId);
	
		return 	BeanCopierUtils.copy(ji, new JobExecInstanceContext());
	}

	@Override
	public List<JobExecInstancePo> queryInstanceList( Long jobId, String startTime, String endTime, String status) {
		// TODO Auto-generated method stub
	
		return jobExecInsMapper.selectByJobid(jobId, startTime, endTime, status);
	}

}
